[SYCLomatic] If viewed from the translation unit, the header file <bits/stdc++.h> is included before SYCL header file, then insert the SYCL header file at the beginning of the main source file of the translation unit. #2790
Conversation
…ncluded, don't insert sycl.hpp at the mainfile Signed-off-by: Jiang, Zhiwei <zhiwei.jiang@intel.com>
Signed-off-by: Jiang, Zhiwei <zhiwei.jiang@intel.com>
There was a problem hiding this comment.
Pull Request Overview
This PR refines the logic for inserting the SYCL header by ensuring it is only added to the main source file when <bits/stdc++.h> is included before SYCL headers, and it also removes several redundant test cleanup commands. The key changes are:
- Removal of the commented-out file removal commands from various test files.
- Addition of a flag and mapping update when <bits/stdc++.h> is encountered in the preprocessor callbacks.
- Updating the global state and header insertion logic in AnalysisInfo to conditionally insert SYCL headers based on inclusion order.
Reviewed Changes
Copilot reviewed 31 out of 31 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| clang/test/dpct/*.cpp | Removed commented out test cleanup commands |
| clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp | Added check to set flag when <bits/stdc++.h> is included |
| clang/lib/DPCT/PreProcessor.cpp | Updated to record files included after encountering <bits/stdc++.h> |
| clang/lib/DPCT/AnalysisInfo.{h,cpp} | Adjusted header insertion logic and flag reset in the main file setter |
| FileInfo->setFirstIncludeOffset(LocInfo.second); | ||
| LastInclusionLocationUpdater Updater(FileInfo, FilenameRange.getEnd()); | ||
|
|
||
| if (FileName == "bits/stdc++.h") { |
There was a problem hiding this comment.
[nitpick] Consider extracting the literal "bits/stdc++.h" into a named constant to improve maintainability and reduce the chances of typos in future modifications.
| if (FileName == "bits/stdc++.h") { | |
| if (FileName == BITS_STDCXX_HEADER) { |
| } | ||
| std::shared_ptr<DpctFileInfo> getMainFile() const { return MainFile; } | ||
| void setMainFile(std::shared_ptr<DpctFileInfo> Main) { MainFile = Main; } | ||
| void setMainFile(std::shared_ptr<DpctFileInfo> Main) { |
There was a problem hiding this comment.
[nitpick] It would be helpful to add a brief comment in setMainFile explaining why the IsAfterBitsStdcxx flag is reset when a new main file is set, to clarify the logic for future maintainers.
Background: if <bits/stdc++.h> is included before the SYCL header file, it results in a compiler error.
The behavior before this PR:
If a file contains CUDA syntax, the tool inserts the SYCL header file in that file. Also, SYCL header file needs to be inserted at the beginning of the main file (to avoid
bits/stdc++.his included before SYCL header file). The solution has an issue:sycl.hppmay be inserted in some pure C++ code without checking ifbits/stdc++.his included or not.This PR will be further refined as follows:
In the translation unit, if
bits/stdc++.his included before the SYCL header file, insert the SYCL header file at the beginning of the main file.